package com.ruoyi.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.entity.vo.PermissionBatchDTO;
import com.ruoyi.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Api("PermissionController    api")
@RequestMapping("/api/permission")
public class PermissionController extends BaseController {
    @Autowired
    private PermissionService permissionService;

    @GetMapping("/check")
    @ApiOperation("权限校验接口")
    public AjaxResult checkPermission(
            @RequestParam Integer refType,
            @RequestParam Long refId,
            @RequestParam String action) {

        return AjaxResult.success(
                permissionService.checkPermission(refType, refId, action)
        );
    }

    @ApiOperation(("获取权限"))
    @PostMapping("/getPermissions")
    public AjaxResult getPermissions(@RequestBody PermissionBatchDTO dto) {
        Map<String, Object> config = permissionService.getPermissionsConfig(dto);
        System.out.println("获取的权限         ："+config);
        return AjaxResult.success(config);
    }

    @ApiOperation("保存权限")
    @PostMapping("/batchSave")
    public AjaxResult batchSave(@RequestBody PermissionBatchDTO dto) {
        permissionService.savePermissions(dto);
        System.out.println("-----------------------权 限--------Received DTO:  ------------------"     +dto);
        return AjaxResult.success("权限保存成功");
    }
}
